package xiang.weiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xiang.weiyu.common.BaseContext;
import xiang.weiyu.common.R;
import xiang.weiyu.common.ValidateCodeUtils;
import xiang.weiyu.dto.Code;
import xiang.weiyu.pojo.User;
import xiang.weiyu.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取验证码
     *
     * @param user
     * @return R<String>
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user) {
        //模拟验证码生成
        String yzm = ValidateCodeUtils.generateValidateCode(6)+"";
        //存入session
//        request.getSession().setAttribute("yzm", yzm);

        //存入Redis中
        redisTemplate.opsForValue().set(user.getPhone(),yzm,5, TimeUnit.MINUTES);

        log.info("验证码为: " + yzm);
        return R.success("验证码发送成功");
    }

    /**
     * 用户登录功能
     *
     * @param code
     * @param request
     * @return R<String>
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody Code code, HttpServletRequest request) {
        //从session中获取验证码
//        String yzm = request.getSession().getAttribute("yzm") + "";

        String yzm = (String) redisTemplate.opsForValue().get(code.getPhone());
        log.info("redis中获取的验证码为: " + yzm);
        //判断
        if (yzm.equals(code.getCode())) {
            //验证码验证通过,登陆成功
            LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
            qw.eq(code.getPhone() != null, User::getPhone, code.getPhone());
            if (userService.count(qw) > 0) {
                //用户已存在,将id存入session,登陆成功
                User user = userService.getOne(qw);
                request.getSession().setAttribute("user", user.getId());
            } else {
                //用户不存在,创建用户并将id存入session,登陆成功
                User user2 = new User(code.getPhone());
                userService.save(user2);
                request.getSession().setAttribute("user", user2.getId());
            }
            //如果用户登陆成功,删除redis中的验证码缓存数据
            redisTemplate.delete(code.getPhone());

            return R.success("用户登陆成功");
        } else {
            //登陆失败
            return R.error("验证码错误，登录失败");
        }
    }

    /**
     * 用户退出登录功能
     *
     * @param request
     * @return R<String>
     */
    @PostMapping("/loginout")
    public R<String> loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        BaseContext.release();
        return R.success("退出登录成功");
    }
}
